'use strict';
var config = require('../config');
var JsonDataStr = require('./jsonDataStr');

exports.manager_sticker_list_page = function(req, res) {
	var AppId = config.AppId;
	var page = req.query.page;
	var size = req.query.size;
	if (!page) {
		page = 1;
	}
	if (!size) {
		size = 10;
	}
	var path = '/v1/imageShare/sticker/list?AppId=' + AppId + '&page=' + page + '&size=' + size;
	JsonDataStr.get(path, function(callback) {
		if (!callback.status) {
			return res.render('admin/msg', {
				title: '贴纸列表',
				userinfo: req.session.sys_user,
				info: '系统错误:' + callback.message,
				btnOKUrl: null,
				btnOKName: null,
				btnCancelUrl: '/admin/index',
				btnCancelName: '返回'
			});
		}
		res.render('admin/sticker', {
			title: '贴纸列表',
			userinfo: req.session.sys_user,
			stickers: callback.resources,
			total: callback.total,
			thisPage: page
		});
	});
}

exports.manager_sticker_add_page = function(req, res) {
	res.render('admin/stickerAdd', {
		title: '新增贴纸',
		userinfo: req.session.sys_user
	})
}

//添加sticker
exports.manager_sticker_add = function(req, res) {
	var AppId = config.AppId;
	var url = req.body.url;
	var indexNum = req.body.indexNum;
	var postData = JSON.stringify({
		AppId: AppId,
		url: url,
		indexNum: indexNum
	});
	var path = '/v1/imageShare/sticker/add';
	JsonDataStr.post(postData, path, function(row) {
		if (!row.status) {
			return res.json({
				status: false,
				message: '系统错误:' + row.message
			});
		}
		res.json({
			status: true,
			resources: row.resources
		});
	});
};

exports.manager_sticker_edit_page = function(req, res) {
	var sticker_id = req.query.sticker_id;
	if (!sticker_id) {
		return res.json({
			status: false,
			message: 'no sticker_id'
		});
	}
	var path = '/v1/imageShare/sticker/find?imageShareSticker_id=' + sticker_id;
	JsonDataStr.get(path, function(row) {
		if (!row.status) {
			return res.json({
				status: false,
				message: '系统错误:' + row.message
			});
		}
		res.render('admin/stickerEdit', {
			title: '修改贴纸',
			userinfo: req.session.sys_user,
			sticker: row.resources
		});
	});
}

//系统管理员修改sticker内容
exports.manager_sticker_edit = function(req, res) {
	var sticker_id = req.body.sticker_id;
	if (!sticker_id) {
		return res.json({
			status: false,
			message: 'no sticker_id'
		});
	}
	var url = req.body.url;
	var indexNum = req.body.indexNum;
	var postData = JSON.stringify({
		imageShareSticker_id: sticker_id,
		url: url,
		indexNum: indexNum
	});
	var path = '/v1/manager/imageShare/sticker/edit';
	JsonDataStr.post(postData, path, function(row) {
		if (!row.status) {
			return res.json({
				status: false,
				message: '系统错误'
			});
		}
		res.json({
			status: true
		});
	})
}

//系统管理员删除sticker
exports.manager_sticker_del = function(req, res) {
	var sticker_id = req.body.sticker_id;
	if (!sticker_id) {
		return res.json({
			status: false,
			message: 'no sticker_id'
		});
	}
	var path = '/v1/manager/imageShare/sticker/delete';
	var postData = JSON.stringify({
		imageShareSticker_id: sticker_id
	});
	JsonDataStr.post(postData, path, function(row) {
		if (!row.status) {
			return res.json({
				status: false,
				message: '系统错误'
			});
		}
		res.json({
			status: true
		});
	})
}